#coding=utf-8

from uliweb.orm import *
from uliweb.utils.common import get_var

def get_modified_user():
    from uliweb import request
    if request and request.user:
        return request.user.id
    else:
        return None

class System(Model):
    __verbose_name__ = u'系统'
    
    sys_id = Field(CHAR, verbose_name='编号', max_length=6, required=True, unique=True, index=True)
    name = Field(CHAR, verbose_name='中文名称', max_length=255, required=True)
    abbr = Field(CHAR, verbose_name='英文简写', max_length=20, required=True)
    stage = Field(CHAR, verbose_name='所处阶段', max_length=20, choices=get_var('PARA/SYSTEM_STAGE'), required=True)
    reason = Field(TEXT, verbose_name='运维未移交原因')
    transfer_date = Field(datetime.date, verbose_name='运维移交或关闭时间')
    kfzx = Reference('kfzx', verbose_name='开发中心', collection_name='kfzx_systems')
    contact = Field(TEXT, verbose_name='联系人及方式')
    memo = Field(TEXT, verbose_name='备注')
    modified_date = Field(datetime.datetime, verbose_name='修改时间', auto_now=True, auto_now_add=True)
    modified_user = Reference('user', verbose_name='修改人', default=get_modified_user, auto=True, auto_add=True)
    manager = Reference('user', verbose_name='管理员', collection_name='manager_systems')
    users = ManyToMany('user', verbose_name='维护人员', collection_name='users_systems')
    observers = ManyToMany('user', verbose_name='关注人员', collection_name='observers_systems')
    sys_type =  Field(CHAR, verbose_name='系统类别', max_length=1, choices=get_var('PARA/SYSTEM_TYPE'))   
    selfdev_if = Field(bool, verbose_name='是否参与系统自主率考核',default=True)
    rate = Field(CHAR, max_length=1, verbose_name='系统评级', choices=get_var('PARA/SYSTEM_RATE'))
    construction = Field(CHAR, max_length=1, verbose_name='系统建设情况', choices=get_var('PARA/SYSTEM_CONSTRUCTION'))
    
    def __unicode__(self):
        return u'%s-%s(%s)' % (self.sys_id, self.abbr, self.name)
    
    def get_url(self):
        return u'<a href="/systems/%d" title="%s">%s</a>' % (self.id, self.name, self.abbr)
    
    class Meta:
        group_field = 'kfzx'
    
    class AddForm:
        fields = ('sys_id', 'name', 'abbr', 'stage', 'transfer_date', 
            'reason', 'kfzx','sys_type', 'rate', 'construction', 'contact', 'memo')
    
    class DetailView:
        fields = ('sys_id', 'name', 'abbr', 'stage', 'transfer_date', 
            'reason', 'kfzx', 'sys_type', 'rate', 'construction', 'contact', 'memo', 'manager', 'users', 'observers',
            'modified_date', 'modified_user','selfdev_if')
        
    class Table:
        fields = [
            {'name':'sys_id', 'width':50},
            {'name':'name',  'width':200},
            {'name':'abbr',  'width':80},
            {'name':'sys_type'},
            {'name':'stage',  'width':100},
            {'name':'transfer_date',  'width':110},
            {'name':'kfzx', 'width':120},
            {'name':'rate', 'width':60},
            {'name':'construction', 'width':60},
#            {'name':'contact'},
#            {'name':'manager'},
#            {'name':'users'},
#            {'name':'observers'},
        ]
    
    def save(self):
        from uliweb import settings
        
        User = get_model('user')
        if not self.id:
            #检查是否用户存在
            manager = User.get(User.c.username == self.sys_id)
            if not manager:
                manager = User(username=self.sys_id, is_manager=True, kfzx=self.kfzx, department='2')
                manager.save()
                manager.set_password(settings.PARA.DEFAULT_PASSWORD)
            self.manager = manager
        
        r = super(System, self).save()
        #添加用户
        if not self.users.has(self.manager):
            self.users.add(self.manager)
            r = True
        return r
    
    @classmethod        
    def get_systems(cls, not_in=('5',)):
        if not_in:
            condition = ~cls.c.sys_type.in_(not_in)
        else:
            condition = None
        return cls.filter(condition).order_by(cls.c.kfzx)
    
#class Resource(Model):
#    __verbose_name__ = u'系统资源'
#    
#    system = Reference('system', verbose_name='系统', required=True, collection_name='sys_resources')
#    kfzx = Reference('kfzx', verbose_name='开发中心', required=True, collection_name='kfzx_resources')
#    inner_number = Field(int, verbose_name='中心投入人数')
#    outter_number = Field(int, verbose_name='公司投入人数')
#    year_month = Field(int, verbose_name='年月')
#    modified_date = Field(datetime.datetime, verbose_name='修改时间', auto_now=True, auto_now_add=True)
#    modified_user = Reference('user', verbose_name='修改人', default=get_modified_user)
#    
#    @classmethod
#    def OnInit(cls):
#        Index('resource_indx', cls.c.system, cls.c.year_month, unique=True)
#    
#    